/*
 * SelectionExpressionDescription.java
 * 
 * last update: 30.01.2010 by Vlad Badoiu
 * 
 * author:	Alec(panovici@elcom.pub.ro)
 * 
 * Obs:
 */

package engine;

import java.util.*;


/**
 * This is what the standard calls a concatenation
 */
@SuppressWarnings("serial")
class SelectionExpressionDescription
implements ExpressionDescription, AssignableSelection
{

	Vector<ExpressionDescription> chunks;

	SelectionExpressionDescription(){
		chunks = new Vector<ExpressionDescription>();
	}

	void addChunk(ExpressionDescription ck){
		chunks.addElement(ck);
	}


	public Expression instantiate(NameSpace ns)throws ParseException{
		return new SelectionExpression(ns, chunks);
	}

	/**
	 *  convert this to a wire assignable selection expression, 
	 *  i.e tells all the GenericSelectorDescriptions to
	 *  generate ContBitSelects at instantiaition time
	 */
	public void toContSelection(byte strength)throws ParseException{
		for(Enumeration<ExpressionDescription> e = chunks.elements() ; e.hasMoreElements() ;){
			Object o = e.nextElement();
			try {
				((AssignableSelection) o).toContSelection(strength);
			}  catch (ClassCastException cex) {
				throw new ParseException ("this type of expression cannot be continuously assigned: " +
						o);
			}

		}
	}

	public void toContSelection() throws ParseException{
		toContSelection(Strength.defaultStrength);
	}

	public String toString(){
		String result = "{ ";
		Enumeration<ExpressionDescription> e = chunks.elements();
		while(true){
			result += e.nextElement();
			if(e.hasMoreElements())result += ", ";
			else break;
		}
		result += "}";
		return result;
	}

	Enumeration<ExpressionDescription> elements(){
		return chunks.elements();
	}

}




